import util from '../utils/util';
import request from '../utils/request';
import api from '../config/api';

function checkLogin() {
  return new Promise(function (resolve, reject) {
    // 读取本地保存的用户令牌
    const userToken = wx.getStorageSync('token');
    if (userToken) {
      // 如果登录令牌存在
      // 检查微信登录回话是否过期
      util.wxCheckSession().then(() => {
        // 如果登录没有过期，返回用户已经登录
        resolve(true);
      }).catch(() => {
        wx.getStorageSync('token', '');
        // 否则登录时效
        reject(false);
      });
    } else {
      // 如果用户登录令牌不存在，返回未登录
      reject(false);
    }
  });
}

function login(userInfo) {
  let code = null;
  // 微信登录，获取临时code
  return new Promise((resolve, reject) => {
    util.wxLogin()
      .then(res => {
        code = res.code;

        return request(api.Path.Login, {
          code,
          avatar: userInfo.avatarUrl,
          username: userInfo.nickName
        }, 'POST')
      })
      .then((res) => {
        console.log(res);
        debugger;
        const token = res.data.token;
        const vip = res.data.vip;
        // 登录成功之后，将令牌和用户信息写入缓存
        wx.setStorageSync('token', token);
        wx.setStorageSync('vip', vip);
        wx.setStorageSync('userInfo', userInfo);
        getApp().globalData.userInfo = wx.getStorageSync('userInfo');
        resolve(res); // 返回用户登录结果
      })
      .catch(err => reject(err));
  });
}

function userInfo() {
  return new Promise((resolve, reject) => {
    request(api.Path.UserInfo, {}, 'GET')
    .then(res => resolve(res))
    .catch(err => reject(err));
  });
}
function address(res){
  return new Promise((resolve, reject) => {
    request(api.Path.Address, res, 'POST')
      .then(res => {
        debugger;
        resolve(res)
        })
      .catch(err => reject(err));
  });  
}
function lotteryList(lotteryType){
  return new Promise((resolve, reject) => {
    request(api.Path.LotteryList, {
      lotteryType: lotteryType
    }, 'GET')
      .then(res => {
        debugger;
        resolve(res)
      })
      .catch(err => reject(err));
  });  
}
function lotteryAddress(lotteryId){
  return new Promise((resolve, reject) => {
    request(api.Path.LotteryAddress, {
      lotteryId: lotteryId
    }, 'GET')
      .then(res => {
        debugger;
        resolve(res)
      })
      .catch(err => reject(err));
  });    
}
function buyVip(){
  return new Promise((resolve, reject) => {
    request(api.Path.BuyVip, {}, 'GET')
      .then(res => {
        debugger;
        resolve(res)
      })
      .catch(err => reject(err));
  });    
}
function withdraw() {
  return new Promise((resolve, reject) => {
    request(api.Path.withdraw, {}, 'POST')
      .then(res => resolve(res))
      .catch(err => reject(err));
  })
}
function withdrawRecords() {
  return new Promise((resolve, reject) => {
    request(api.Path.withdraw, {}, 'GET')
      .then(res => resolve(res))
      .catch(err => reject(err));
  })
}
export default {
  checkLogin,
  login,
  userInfo,
  address,
  lotteryList,
  lotteryAddress,
  buyVip,
  withdraw,
  withdrawRecords
}